package gamagora.game.states
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	import gamagora.game.entity.OSBird;
	import gamagora.game.entity.OSMonkey;
	import gamagora.game.resources.OSImages;
	import gamagora.game.resources.OSSounds;
	
	import org.flixel.FlxButton;
	import org.flixel.FlxG;
	import org.flixel.FlxObject;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	import org.flixel.FlxText;
	
	public class OSMenuState extends FlxState
	{
		private static const BACK1_SPEED:Number = 50;
		private static const BACK2_SPEED:Number = 150;
		
		private var _title:FlxSprite;
		private var _monkey:OSMonkey;
		private var _readyToStart:Boolean;
		private var _anyKeyText:FlxText;
		private var _textAlphaUp:Boolean;
		private var _birds:Vector.<OSBird>;
		private var _blackFade:FlxSprite;
		private var _completeFade:Boolean;
		
		//Backgrounds
		private var _decal:Number;
		private var _back1:FlxSprite;
		private var _back2:FlxSprite;
		private var _back3:FlxSprite;
		private var _back4:FlxSprite;
		private var _back5:FlxSprite;
		private var _back6:FlxSprite;
		
		public override function create():void
		{
			//Background
			add(new FlxSprite(0, 0, OSImages.TITLE_BACKGROUND));
			
			//Backgrounds
			_back1 = new FlxSprite(0, -300, OSImages.BACKGROUND_1_1);
			_back2 = new FlxSprite(960, -300, OSImages.BACKGROUND_1_2);
			_back3 = new FlxSprite(1920, -300, OSImages.BACKGROUND_1_3);
			
			_back4 = new FlxSprite(0, -250, OSImages.BACKGROUND_2_2);
			_back5 = new FlxSprite(960, -250, OSImages.BACKGROUND_2_4);
			_back6 = new FlxSprite(1920, -250, OSImages.BACKGROUND_2_5);
			
			add(_back1);
			add(_back2);
			add(_back3);
			add(_back4);
			add(_back5);
			add(_back6);
			
			//Title
			_title = new FlxSprite(148, -248, OSImages.MAIN_TITLE);
			add(_title);
			
			//Monkey
			_monkey = new OSMonkey(460, 462, false, 0, 40, true);
			_monkey.animOnly = true;
			_monkey.canCry = false;
			_monkey.facing = FlxObject.LEFT;
			_monkey.play(OSMonkey.IDLE);
			add(_monkey);
			
			//Birds
			_birds = new Vector.<OSBird>();
			var bx:Number = 246;
			var by:Number = 148; 
			
			var bird:OSBird = new OSBird(bx, by);
			bird.animOnly = true;
			bird.speedX = bird.speedY = 200;
			_birds.push(bird);
			
			bird = new OSBird(bx - 20, by + 84);
			bird.animOnly = true;
			bird.speedX = bird.speedY = 200;
			_birds.push(bird);
			
			bird = new OSBird(bx + 358, by - 76);
			bird.animOnly = true;
			bird.speedX = bird.speedY = 200;
			_birds.push(bird);
			
			bird = new OSBird(bx + 438, by + 14);
			bird.animOnly = true;
			bird.speedX = bird.speedY = 200;
			_birds.push(bird);
			
			bird = new OSBird(bx + 528, by + 44);
			bird.animOnly = true;
			bird.speedX = bird.speedY = 200;
			_birds.push(bird);
			
			for(var i:uint=0;i<_birds.length;i++)
			{
				_birds[i].y -= 348;
				add(_birds[i]);
			}
			
			//Text
			_readyToStart = false;
			//_anyKeyText = new FlxText(305, 400, 300, "Appuyer sur une touche").setFormat(null, 20, 0xff000000, "center", 0x233e58);
			_anyKeyText = new FlxText(305, 400, 300, "Press any key to start").setFormat(null, 20, 0xff000000, "center");
			add(_anyKeyText);
			_anyKeyText.visible = false;
			
			//Black fade
			_blackFade = new FlxSprite();
			_blackFade.makeGraphic(FlxG.width, FlxG.height, 0xff000000);
			add(_blackFade);
			
			OSSounds.playMusic();
		}
		
		public override function update():void
		{
			super.update();
			
			if(!_completeFade)
			{
				_blackFade.alpha -= FlxG.elapsed;
				if(_blackFade.alpha <= 0)
				{
					_completeFade = true;
				}
			}
			
			if(_title.y < 100)
			{
				_decal = FlxG.elapsed * 100;
				_title.y += _decal;
				for(var i:uint=0;i<_birds.length;i++)
				{
					_birds[i].y += _decal;
				}
				if(_title.y >= 100)
				{
					_monkey.play(OSMonkey.START_RUN);
					_anyKeyText.visible = true;
					_readyToStart = true;
				}
			}
			else
			{
				if(_readyToStart && FlxG.keys.any())
				{
					for(i=0;i<_birds.length;i++)
					{
						_birds[i].play(OSBird.START_FLY);
					}
					_readyToStart = false;
					FlxG.fade(0xff000000, 3, goToIntro);
				}
				
				if(_monkey.finished && _monkey.curAnim.name == OSMonkey.START_RUN)
				{
					_monkey.play(OSMonkey.RUN);
				}
				
				if(_textAlphaUp)
				{
					_anyKeyText.alpha += FlxG.elapsed;
					_textAlphaUp = _anyKeyText.alpha < 1;
				}
				else
				{
					_anyKeyText.alpha -= FlxG.elapsed;
					_textAlphaUp = _anyKeyText.alpha <= 0;
				}
				
				_decal = FlxG.elapsed * BACK1_SPEED;
				checkBackPosition(_back1, _decal);
				checkBackPosition(_back2, _decal);
				checkBackPosition(_back3, _decal);
				
				_decal = FlxG.elapsed * BACK2_SPEED;
				checkBackPosition(_back4, _decal);
				checkBackPosition(_back5, _decal);
				checkBackPosition(_back6, _decal);
			}
 		}
		
		private function checkBackPosition(sprite:FlxSprite, decal:Number):void
		{
			sprite.x -= decal;
			
			if(sprite.x <= -960)
			{
				sprite.x = 1920;
			}
		}
		
		private function goToIntro():void
		{
			FlxG.switchState(new OSIntroState());
		}
	}
}
